<template>
  <div>
    <!-- 面包 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>复仇管理</el-breadcrumb-item>
      <el-breadcrumb-item>复仇列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input
            placeholder="请输入内容"
            class="input-with-select"
            v-model="queryInfo.query"
            clearable
            :select-when-unmatched="false"
            @keyup.enter.native="getuserlist"
            @clear="getuserlist"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getuserlist"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="5"
          ><el-button type="primary" @click="dialogVisible = true"
            >揭榜按钮</el-button
          ></el-col
        >
      </el-row>
      <!-- 表格 -->
      <el-table :data="userlist" border style="width: 100%">
        <el-table-column type="index" label="#" width="80"> </el-table-column>
        <el-table-column prop="username" label="姓名" width="180">
        </el-table-column>
        <el-table-column prop="email" label="邮箱" width="180">
        </el-table-column>
        <el-table-column prop="mobile" label="电话" width="180">
        </el-table-column>
        <el-table-column prop="role_name" label="身份" width="180">
        </el-table-column>
        <el-table-column label="状态" width="180">
          <template slot-scope="scope">
            <!-- {{scope.row}}   -->
            <el-switch
              v-model="scope.row.mg_state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="usere(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="putt(scope.row.id)"
            ></el-button>
            <el-tooltip content="分配任务" placement="top" :enterable="false">
              <!-- 设置按钮 -->
              <el-button type="success" icon="el-icon-setting"></el-button>
            </el-tooltip>
            <!-- 删除按钮 -->
            <el-button
              type="warning"
              icon="el-icon-delete"
              @click="dele(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 3, 4]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加用户对话框 -->
    <el-dialog
      title="揭榜人员"
      :visible.sync="dialogVisible"
      width="50%"
      @close="addclearn"
    >
      <el-form
        :model="addForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
      >
        <!-- <el-input v-model="queryInfo.password"></el-input>
          <el-input v-model="queryInfo.email"></el-input>
          <el-input v-model="queryInfo.mobile"></el-input> -->
        <el-form-item label="复仇者名" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>

        <el-form-item label="复仇者密码" prop="password">
          <el-input v-model="addForm.password" type="password"></el-input>
        </el-form-item>

        <el-form-item label="复仇者邮箱" prop="email">
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>

        <el-form-item label="复仇者电话" prop="mobile">
          <el-input v-model="addForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="adduser">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改对话框 -->

    <el-dialog
      title="修改"
      :visible.sync="edit"
      width="50%"
      @close="editclearn"
    >
      <el-form
        :model="editfrom"
        :rules="editrules"
        ref="editForm"
        label-width="100px"
      >
        <el-form-item label="用户名">
          <el-input v-model="editfrom.username" disabled></el-input>
        </el-form-item>

        <el-form-item label="电话" prop="mobile">
          <el-input v-model="editfrom.mobile"></el-input>
        </el-form-item>

        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editfrom.email"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="edit = false">取 消</el-button>
        <el-button type="primary" @click="editt">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created() {
    this.getuserlist();
  },
  data() {
    // 验证邮箱的规则
    var checkEmail = (rule, value, cb) => {
      // 验证邮箱的正则表达式
      const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;

      if (regEmail.test(value)) {
        // 合法的邮箱
        return cb();
      }

      cb(new Error("请输入合法的邮箱"));
    };

    // 验证手机号的规则
    var checkMobile = (rule, value, cb) => {
      // 验证手机号的正则表达式
      const regMobile = /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;

      if (regMobile.test(value)) {
        return cb();
      }

      cb(new Error("请输入合法的手机号"));
    };

    return {
      queryInfo: {
        query: "",
        pagenum: 1, //当前页码 不可为空
        pagesize: 2, //当前条数
      },

      // 保存数据
      userlist: [],
      total: 0,
      // 控制添加用户的显示与隐藏
      dialogVisible: false,
      // 添加用户的表单数据
      addForm: {},
      // 修改用户的显示与隐藏
      edit: false,
      //查询的id信息
      editfrom: {},
      // 添加用户的验证规则对象
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 1, max: 5, message: "请输入3-5的长度", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 5, max: 15, message: "密码长度5-15位", trigger: "blur" },
        ],
        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          { validator: checkEmail, trigger: "blur" },
        ],
        mobile: [
          { required: true, message: "请输入电话", trigger: "blur" },
          { validator: checkMobile, trigger: "blur" },
        ],
      },
      // 修改规则
      editrules: {
        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          { validator: checkEmail, trigger: "blur" },
        ],
        mobile: [
          { required: true, message: "请输入电话", trigger: "blur" },
          { validator: checkMobile, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    async getuserlist() {
      const { data: res } = await this.$http.get("users", {
        params: this.queryInfo,
      });
      console.log(res);
      if (res.meta.status !== 200) return this.$message.error("获取用户失败");
      // this.$message.success("获取成功");
      this.userlist = res.data.users;
      this.total = res.data.total;
    },
    handleSizeChange(newsize) {
      // console.log(newsize);
      this.queryInfo.pagesize = newsize;
      this.getuserlist();
    },
    handleCurrentChange(newpage) {
      // console.log(newpage);
      this.queryInfo.pagenum = newpage;
      this.getuserlist();
    },
    async usere(val) {
      // console.log(val);
      const { data: res } = await this.$http.put(
        `users/${val.id}/state/${val.mg_state}`
      );
      // console.log(res);
      if (res.meta.status !== 200) {
        val.mg_state = !val.mg_state;
        return this.$message.error("更新状态失败");
      }
      this.$message.success("ok");
    },
    // 添加重置
    addclearn() {
      this.$refs.ruleForm.resetFields();
    },
    // 添加用户
    adduser() {
      this.$refs.ruleForm.validate(async (valid) => {
        // console.log(valid);
        if (!valid) return;
        const { data: res } = await this.$http.post("users", this.addForm);
        // console.log(res);
        if (res.meta.status !== 201) return this.$message.error("添加用户失败");
        this.$message.success("添加用户成功");
        this.dialogVisible = false;
        this.getuserlist();
      });
    },
    // 编辑修改
    async putt(val) {
      this.edit = true;
      // console.log(val);
      const { data: res } = await this.$http.get(`users/${val}`);
      console.log(res);
      if (res.meta.status !== 200)
        return this.$message.error("编辑用户信息失败");
      this.editfrom = res.data;
    },
    // 重置修改表单
    editclearn() {
      this.$refs.editForm.resetFields();
    },
    // 修改预验证
    editt() {
      this.$refs.editForm.validate(async (valid) => {
        // console.log(valid);
        if (!valid) return;
        const { data: res } = await this.$http.put(
          "users/" + this.editfrom.id,
          {
            email: this.editfrom.email,
            mobile: this.editfrom.mobile,
          }
        );
        // console.log(res);
        if (res.meta.status !== 200) return this.$message.error("更新数据失败");
        this.$message.success("更新信息成功");
        this.edit = false;
        this.getuserlist();
      });
    },
    async dele(id) {
      console.log(id);
      const resv = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
        // catch捕获错误信息
      ).catch((err) => err);
      // 删除确认成功，结果为字符串confirm
      // 删除取消，结果为字符串cancel
      // console.log(res);

      if (resv !== "confirm") {
        return this.$message.success("取消删除");
      }

      const { data: res } = await this.$http.delete(`users/${id}`);
      console.log(res);

      if (res.meta.status !== 200) return this.$message.error("删除用户失败");

      this.$message.success("删除成功");

      this.getuserlist();
    },
  },
};
</script>

<style lang="less" scoped>
.box-card {
  height: 500px;
  margin-top: 20px;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.55) !important;
}
.el-breadcrumb {
  margin-bottom: 10px;
  font-size: 18px;
}
.el-card {
  margin-top: 20px;
}
.box-card {
  box-shadow: 1px 1px 1px 1px rgba(0, 0, 0, 0.15) !important;
}
.el-table {
  margin-top: 30px;
}
.el-pagination {
  margin-top: 40px;
  font-size: 20px;
}
</style>